# -*- coding:utf-8 -*-

from datetime import datetime

from . import db
from .utils.utils import random_uuid


class BaseModel(object):
    """模型基类，为每个模型补充创建时间与更新时间"""
    create_time = db.Column(db.DateTime, default=datetime.now, comment="创建时间")  # 记录的创建时间
    last_update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment="最后更新时间")  # 记录的更新时间


class Task(BaseModel, db.Model):
    """造数据接口任务表"""
    __tablename__ = "zfs_all_api"

    id = db.Column(db.VARCHAR(64), primary_key=True, autoincrement=False, default=random_uuid(), comment="接口id")
    description = db.Column(db.String(500), unique=False, nullable=False, comment="接口描述")
    method_name = db.Column(db.String(128), unique=False, nullable=False, comment="造数据类名+方法名，用.连接")  # 加密的密码
    method_path = db.Column(db.String(128), unique=False, nullable=False, comment="造数据方法所在的类的路径，用于动态导包")  # 加密的密码
    method_category = db.Column(db.String(24), unique=False, nullable=False, comment="基础数据、基础配置、业务数据")  # 加密的密码
    status = db.Column(db.CHAR(1), unique=False, nullable=False, comment="状态，0：可用，1：不可用.")
    run_num_tips = db.Column(db.String(12), unique=False, nullable=False, comment="执行次数提示")

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "description": self.description,
            "category": self.method_category,
            "status": self.status,
            "run_num_tips": self.run_num_tips
        }
        return resp_dict


class Schedule(BaseModel, db.Model):
    """进度表"""
    __tablename__ = "zfs_schedule_task"

    id = db.Column(db.VARCHAR(64), primary_key=True, autoincrement=False, default=random_uuid(), comment="id")
    task_id = db.Column(db.VARCHAR(64), db.ForeignKey("zfs_all_api.id"), comment="未执行完成的任务id")  # Task表中的id，外键
    user_ip = db.Column(db.VARCHAR(24), nullable=False, comment="调用者ip，用于断点续跑用户身份确认")
    server_ip = db.Column(db.VARCHAR(24), nullable=False, comment="环境ip")
